<template>
    <div>
        <div class="outmain ba_f heighthv pad_20 bor_rad5">
            <div class="shuju_title mar_b20">
                <div class="shuju_title_text">
                    <span>新建页面链接跳转</span>
                </div>
            </div>
            <div class="mar_t20 flex-bet flex-y-top">
                <div class="flex-g-1">
                    <el-form ref="form" :model="form" label-width="180px">
                        <el-form-item label="链接名称" :required="true" prop="title">
                          <div class="flex">
                            <el-input v-model="form.title"></el-input>
                          </div>
                        </el-form-item>
                        <el-form-item label="页面选择">
                          <div class="flex">
                            <el-input v-model="form.name" disabled></el-input>
                            <el-button @click="handleEdit" class="mar_l20">选择页面</el-button>
                          </div>
                        </el-form-item>
                        <el-form-item label="有效期限">
                            <el-radio-group v-model="form.is_expire">
                              <el-radio :label="false">永久有效</el-radio>
                              <el-radio :label="true">有效时间</el-radio>
                            </el-radio-group>
                            <block v-show="form.is_expire==true">
                              <el-input-number v-model="form.expire_time" controls-position="right" :min="1" :max="1000000" class="mar_l20"></el-input-number>
                              <span class="mar_l10">天</span>
                            </block>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>
        <div class="mar_t20 ba_f pad_20 t_c">
            <el-button type="primary" @click="submitForm">保存</el-button>
        </div>
      <urlSelect ref="urlSelect" @getUrl='getUrl'></urlSelect>
    </div>
</template>
<script>
    import {postCreateUrl,} from "@/api/plug";
    import urlSelect from "./components/urlSelect";

    export default {
        created() {
            // if (this.$route.query.id) {
            //     this.id = this.$route.query.id
            //     this.type = this.$route.query.type
            //     this.edit()
            // }
        },
        components: {urlSelect},
        data() {
            return {
                form: {
                  path: '',
                  scene: '',
                  is_expire: false,
                  expire_time: '',
                  title: '',
                  name:'',
                },
                id:'',
            }
        },
        methods: {
            handleEdit() {
              this.$refs["urlSelect"].cateShown = [true];
              this.$refs["urlSelect"].isShown = true;
            },
          getUrl(url){
            // console.log(url)
            if(url){
              this.form.path = url.select.url
              this.form.name = url.select.name
            }
          },
            async submitForm() {
                this.$refs["form"].validate(async (valid) => {
                    if (valid) {
                        const {msg} = await postCreateUrl(this.form);
                        this.$baseMessage(msg, "success");
                        // this.$router.go(-1)
                        this.$router.push({path: '/linkslist'})
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>
